#include "operation.h"
#include <limits>

int get_number_input()
{
    int input = -1;
    std::cin >> input;
    while (std::cin.fail())
    {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cin >> input;
    }
    return input;
}

void continues()
{
    show_menu();
    options();
}

void options()
{
    int option;
    // std::cin >> option;
    option = get_number_input();
    switch (option)
    {
    case 1: // 添加联系人
    {
        add(contact);
        pause();
        continues();
        break;
    }
    case 2: // 显示联系人
    {
        show_all(contact);

        pause();
        continues();
        break;
    }
    case 3: // 删除联系人
    {
        delete_option(contact);
        pause();
        continues();
        break;
    }
    case 4: // 查找联系人
    {
        get_by_name(contact);
        pause();
        continues();
        break;
    }
    case 5: // 修改联系人
    {
        modify(contact);
        pause();
        continues();
        break;
    }
    case 6: // 清空联系人
    {
        clear(contact);
        pause();
        continues();
        break;
    }
    case 0: // 退出
    {
        exit();
        break;
    }
    default:
    {
        std::cout << "输入的选项有误，请输入选项(0, 1, 2, 3, 4, 5, 6)：" << std::endl;
        options();
        break;
    }
    }
}

void exit()
{
    std::cout << "退出程序，欢迎下次继续使用~~" << std::endl;
}

void add(Contact *const contact)
{

    std::cout << "1、添加联系人" << std::endl;
    if (contact->size == contact->limit)
    {
        std::cout << "通讯录已满，不能再加入了" << std::endl;
        return;
    }
    std::cout << "********* 添加联系人 *********" << std::endl;

    int number = contact->size + 1;
    std::string name;
    int gender;
    int age;
    std::string phone;
    std::string address;
    std::cout << "请输入姓名：";
    std::cin >> name;
    std::cout << "请输入性别：\n1 - 男 \n0 - 女：";
    std::cin >> gender;
    std::cout << "请输入年龄：";
    std::cin >> age;
    std::cout << "请输入电话：";
    std::cin >> phone;
    std::cout << "请输入地址：";
    std::cin >> address;

    Person person = {number, name, gender, age, phone, address};
    contact->people[contact->size] = person;
    contact->size = contact->size + 1;

    std::cout << "添加成功！" << std::endl;
}

void title_person()
{
    std::cout << "编号\t姓名\t性别\t年龄\t电话\t\t地址" << std::endl;
}

void show_one(const Person *person)
{
    // std::cout << person.number << "\t" << person.name << "\t" << person.gender << "\t" << person.age << "\t" << person.phone << "\t" << person.address << std::endl;
    std::string gender = person->gender == 1 ? "男" : "女";
    std::cout << person->number << "\t" << person->name << "\t" << gender
              << "\t" << person->age << "\t" << person->phone << "\t" << person->address << std::endl;
}

void show_all(const Contact *contact)
{
    // std::cout << "2、显示联系人" << std::endl;
    title_person();
    if (contact->size == 0)
    {
        std::cout << "\t\t联系人列表为空" << std::endl
                  << std::endl;
        return;
    }
    for (int i = 0; i < contact->size; i++)
    {
        show_one(&contact->people[i]);
    }
}

bool valid_exist(const Contact *contact, int number)
{
    bool flag = false;
    for (int i = 0; i < contact->size; i++)
    {
        if (number == contact->people[i].number)
        {
            flag = true;
        }
    }
    return flag;
}

Contact get_by_name(const Contact *contact, std::string name)
{
    const int num_people = contact->size;
    Contact temp;
    for (int i = 0; i < num_people; i++)
    {
        if (contact->people[i].name == name)
        {
            temp.people[temp.size++] = contact->people[i];
        }
    }

    return temp;
}

void delete_option(Contact *contact)
{

    std::cout << "3、删除联系人" << std::endl;
    /*

        1、输入要删除的人名
            1、查找人名所对应的数量，放在一个数组中
            2、如果数量大于1，则输入序号
                如果数量等于1，则删除
                如果数量等于0，则什么也不做，显示删除成功
        2、删除操作，如果是多个人名相同，则根据编号删除
            如果只删除一个，也按编号删除

    */

    std::string name;
    std::cout << "请输入要删除的人名：";
    std::cin >> name;
    Contact temp_contact = get_by_name(contact, name);
    int size = temp_contact.size;
    if (size == 1)
    {
        std::cout << "确定要删除【" << name << "】吗？(Y, N)";
        char option = 'Y';
        std::cin >> option;
        if (option == 'Y' || option == 'y')
        {
            delete_by_id(contact, temp_contact.people[0].number);
            std::cout << "您已经删除【" << name << "】成功！！" << std::endl;
        }
        else
        {
            std::cout << "您的删除行为已经撤销" << std::endl;
        }
    }
    else if (size == 0)
    {
        std::cout << "您已经删除【" << name << "】成功！！" << std::endl;
    }
    else
    {
        std::cout << "##### 您要删除的列表如下：" << std::endl;
        show_all(&temp_contact);
        int number;
        std::cout << "请输入要删除的联系人编号：";
        std::cin >> number;
        if (valid_exist(&temp_contact, number))
        {
            std::cout << "确定要删除编号为【" << number << "】且姓名为【" << name << "】吗？(Y, N)";
            char option = 'Y';
            std::cin >> option;
            if (option == 'Y' || option == 'y')
            {
                delete_by_id(contact, temp_contact.people[0].number);
                std::cout << "您已经删除编号为【" << number << "】且姓名为【" << name << "】成功！！" << std::endl;
            }
            else
            {
                std::cout << "您的删除行为已经撤销!" << std::endl;
            }
        }
        else
        {
            std::cout << "您输入的编号有误，与您要删除的姓名不符合!" << std::endl;
        }
    }
}

void pause()
{
    // std::cout << "请输入任意键继续...";
    // int a;
    // std::cin >> a;
}

void delete_by_id(Contact *contact, int number)
{
    // std::cout << "3、删除联系人" << std::endl;
    /*
        根据number 查找到对应的位置，将后面的元素向前移动一格
        size - 1;
    */
    for (int i = 0; i < contact->size; i++)
    {
        if (number == contact->people[i].number)
        {
            for (int j = i; j < contact->size; j++)
            {
                contact->people[j] = contact->people[j + 1];
            }
            contact->size--;
            break;
        }
    }
}

void get_by_name(const Contact *contact)
{
    std::cout << "4、查找联系人" << std::endl;
    std::string name;
    std::cout << "请输入要查找的人名：";
    std::cin >> name;
    Contact temp_contact = get_by_name(contact, name);
    std::cout << "##### 查找结果如下：" << std::endl;
    show_all(&temp_contact);
}

Person get_person_from_keyboard(int number)
{
    std::string name;
    int gender;
    int age;
    std::string phone;
    std::string address;
    std::cout << "请输入姓名：";
    std::cin >> name;
    std::cout << "请输入性别：\n1 - 男 \n0 - 女：";
    std::cin >> gender;
    std::cout << "请输入年龄：";
    std::cin >> age;
    std::cout << "请输入电话：";
    std::cin >> phone;
    std::cout << "请输入地址：";
    std::cin >> address;

    Person person = {number, name, gender, age, phone, address};
    return person;
}

void modify(Contact *contact)
{
    std::cout << "5、修改联系人" << std::endl;
    /*
        修改联系人，
            先输入一个人名
            根据人名查找整个列表：
                如果size==1 直接修改
                如果size==0 查找的人不存在
                如果size >1 选择序号，修改

    */
    std::cout << "请输入要修改的联系人姓名：" << std::endl;
    std::string name;
    std::cin >> name;
    Contact temp = get_by_name(contact, name);
    int size = temp.size;
    if (size == 1)
    {
        Person person = get_person_from_keyboard(temp.people[0].number);
        modify_by_id(contact, person);
        std::cout << "联系人【" << person.name << "】的信息已经被修改成功！" << std::endl;
    }
    else if (size == 0)
    {
        std::cout << "您要查找的联系人【" << name << "】不存在于通讯录，不能进行修改!" << std::endl;
    }
    else
    {
        // 如果size >1 选择序号，修改
        std::cout << "##### 您要修改的列表如下：" << std::endl;
        show_all(&temp);
        int number;
        std::cout << "请输入要修改的联系人编号：";
        std::cin >> number;
        if (valid_exist(&temp, number))
        {
            std::cout << "确定要修改编号为【" << number << "】且姓名为【" << name << "】吗？(Y, N)";
            char option = 'Y';
            std::cin >> option;
            if (option == 'Y' || option == 'y')
            {
                Person person = get_person_from_keyboard(number);
                modify_by_id(contact, person);
                std::cout << "您已经修改原编号为【" << number << "】且原姓名为【" << name << "】的联系人成功！！" << std::endl;
            }
            else
            {
                std::cout << "您的修改行为已经撤销" << std::endl;
            }
        }
        else
        {
            std::cout << "您输入的编号有误，与您要修改的姓名不符合" << std::endl;
        }
    }
}

void modify_by_id(Contact *contact, Person person)
{
    int number = person.number;
    for (int i = 0; i < contact->size; i++)
    {
        if (number == contact->people[i].number)
        {
            contact->people[i] = person;
            break;
        }
    }
}

void clear(Contact *contact)
{
    std::cout << "6、清空联系人" << std::endl;
    contact->size = 0;
    std::cout << "清空联系人成功！" << std::endl;
}